﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Diagnostics;

static class InstallerRunner
{
    public static void runInstaller(string name)
    {
        ZipFile.ExtractToDirectory(Path.Join("C:\\osi\\InstallerTemp", name + ".ei")
            , Path.Join("C:\\osi\\bin"
            , name));
        using var fileStream = File.OpenRead(Path.Join(Path.Join("C:\\osi\\InstallerTemp"
            , name), "ei","data.json"));
        using var jsonDoc = JsonDocument.Parse(fileStream);
        bool ipa = jsonDoc.RootElement.GetProperty("path").GetBoolean();
        string ec = jsonDoc.RootElement.GetProperty("exculable").GetString();
        string ac = jsonDoc.RootElement.GetProperty("argument").GetString();
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = ec,
                Arguments = ac,
                WorkingDirectory = Path.Join(Path.Join("C:\\osi\\bin"
                , name), "bin"),
                UseShellExecute = false
            }
        };
        if (ipa)
        {
            string currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User) ?? "";
            string newPath = Path.Join("C:\\osi\\bin", name);
            if (!currentPath.Contains(newPath))
            {
                string updatedPath = currentPath + ";" + newPath;
                Environment.SetEnvironmentVariable("PATH", updatedPath, EnvironmentVariableTarget.User);
            }
        }
    }
}
